package o2o.web.actions.shop;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.agent.CityAgentMemory;
import o2o.cache.shop.ShopMemory;
import o2o.cache.shop.ShopTipRelationMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.agent.CityAgent;
import o2o.dao.PO.common.UserType;
import o2o.company.model.ShopInfo;
import o2o.dao.PO.shop.ShopTip;
import o2o.dao.PO.user.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class ShopListTmpAction {

    // 获取店铺下拉框
    @RequestMapping(method = RequestMethod.POST, value = "/admin/shop/tips/select")
    @ResponseBody
    public String user_agent_select(String token, String city) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }

//        //获取城市所有的商圈
//        List<CityAgent> agentList = CityAgentMemory.getInstance().listOfCity(city);
//        if (!ValidateUtil.isAbsList(agentList)) {
//            return AjaxResult.successResult();
//        }
        List<CityAgent> agentList = null;
        if (0 == UserType.ADMIN - user.getUserType()) {
            //商圈登录的只能看到自己商圈里的
            //根据用户账号，获取自己的商圈
            CityAgent cityAgent = CityAgentMemory.getInstance().getByUsername(user.getUsername());
            if (null != cityAgent) {
                agentList = new ArrayList<>();
                agentList.add(cityAgent);
            }
        } else {
            agentList = CityAgentMemory.getInstance().listOfCity(city);
        }

        if (!ValidateUtil.isAbsList(agentList)) {
            return AjaxResult.successResult();
        }
        List<Object> reList = new ArrayList<>();
        for (int i = 0; i < agentList.size(); i++) {
            CityAgent agent = agentList.get(i);
            if (null == agent) {
                continue;
            }

            //获取商圈里的所有楼层
            List<ShopTip> shopTipList = ShopTipMan.tipListOfAgentId(agent.getId());
            if (!ValidateUtil.isAbsList(shopTipList)) {
                Map<String, Object> agentMap = new HashMap<>();
                agentMap.put("id", agent.getId());
                agentMap.put("label", agent.getName());
                agentMap.put("children", null);
                agentMap.put("leaf", 1);
                reList.add(agentMap);
                continue;
            }
            List<Object> shopTipReList = new ArrayList<>();
            for (int j = 0; j < shopTipList.size(); j++) {
                ShopTip tipId = shopTipList.get(j);
                if (null == tipId) {
                    continue;
                }

                //获取楼层对应的店铺
                List<Long> shopIdList = ShopTipRelationMemory.getInstance().getShopListByTipId(tipId.getId());
                if (!ValidateUtil.isAbsList(shopIdList)) {
                    Map<String, Object> tipMap = new HashMap<>();
                    tipMap.put("id", tipId.getId());
                    tipMap.put("label", tipId.getName());
                    tipMap.put("children", null);
                    tipMap.put("leaf", 2);
                    continue;
                }
                List<Object> shopList = new ArrayList<>();
                for (int k = 0; k < shopIdList.size(); k++) {
                    Long shopId = shopIdList.get(k);
                    if (ValidateUtil.isNotID(shopId)) {
                        continue;
                    }
                    ShopInfo shopInfo = ShopMemory.getInstance().getShopById(shopId);
                    if (null == shopInfo) {
                        continue;
                    }
                    Map<String, Object> shopMap = new HashMap<>();
                    shopMap.put("id", shopInfo.getId());
                    shopMap.put("label", shopInfo.getShopName());
                    shopMap.put("leaf", 3);
                    shopList.add(shopMap);
                }

                Map<String, Object> tipMap = new HashMap<>();
                tipMap.put("id", tipId.getId());
                tipMap.put("label", tipId.getName());
                tipMap.put("children", shopList);
                tipMap.put("leaf", 2);
                shopTipReList.add(tipMap);
            }

            Map<String, Object> agentMap = new HashMap<>();
            agentMap.put("id", agent.getId());
            agentMap.put("label", agent.getName());
            agentMap.put("children", shopTipReList);
            agentMap.put("leaf", 1);
            reList.add(agentMap);
        }

        return AjaxResult.successResult(reList.size(), reList);
    }


}
